package jdmx.framework.common.serialization;

import org.jdom.Element;

import jdmx.library.da.DataColumn;
import jdmx.library.da.DataRow;
import jdmx.library.da.DataTable;

public class XmlSerializerForDataTable implements XmlSerializer {
	
	@Override
	public Element serialize(Object object, String elementName) {
		if (object == null) {
			return null;
		}
		if (!(DataTable.class.isAssignableFrom(object.getClass()))) {
			return null;
		}
		DataTable dataTable =  (DataTable) object;
		Element xmlDatatable = new Element(elementName);

		/*
		 * adaugare coloane
		 */
		Element xmlColumns = new Element("columns");
		for (DataColumn column : dataTable.getColumns()) {
			Element xmlColumn = new Element(column.getName());
			xmlColumns.addContent(xmlColumn);
		}
		xmlDatatable.addContent(xmlColumns);

		/*
		 * adaugare row-uri
		 */
		Element xmlRows = new Element("rows");
		for (DataRow dataRow : dataTable.getRows()) {
			Element xmlRow = new Element("row");
			for (DataColumn column : dataTable.getColumns()) {
				Element xmlCell = new Element(column.getName());
				xmlCell.setText(dataRow.getCell(column.getName()).getValue().toString());
				xmlRow.addContent(xmlCell);
			}
			xmlRows.addContent(xmlRow);
		}
		xmlDatatable.addContent(xmlRows);
		
		return null;
	}

	@Override
	public DataTable deserialize(Element element) {
		return null;
	}

}
